package com.kongge.formlike.service.rule;

import com.kongge.formlike.service.check.ICheckItem;

import java.util.List;

/**
 * author:kongge
 * date:2018/7/21
 * layout:
 * description:
 */

public class RuleLength extends AbsRule {

    private int length;

    @Override
    public void setParam(List<Object> paramList) {
        super.setParam(paramList);
        if (paramList != null && paramList.size() > 0) {
            Object item = paramList.get(0);
            if (item instanceof Integer) {
                length = (Integer) item;
            } else if (item instanceof Double) {
                Double d = (Double) item;
                length = d.intValue();
            }
        }
    }

    @Override
    public boolean check(ICheckItem checkItem) {
        if (length <= 0) {
            return true;
        }
        if (checkItem != null) {
            String checkStr = checkItem.getCheckContent();
            if (checkStr == null) {
                return false;
            }
            return checkStr.length() == length;
        }
        return false;
    }

    public int getLength() {
        return length;
    }
}
